/**
 * 处理钱
 */
const Index = {
	splitToNum : v => {
		if( window.$fn.isString(v) ){
			if(v.includes(',')){
				return Number(v.replace(/,/g,''))
			}else{
				return Number(v)
			}
		}else {
			return v
		}
	},
	sum( data, field='money' ) {
		let total = 0
		if(window.$fn.hasArray(data)){
			data.forEach(v=>{
				let m = this.getNum( v[field] ) || 0
				total += m
			})
		}
		return total
	},
	money : v => {
		if( window.$fn.isNumber( v ) ){
			return v.toFixed(2)
		}else if( window.$fn.isString(v) ){
			return isNaN( +v ) ? null : v.toFixed(2)
		}else{
			return null
		}
	},
	getNum : v => {
		if( window.$fn.isNumber( v ) ){
			return v
		}else if( window.$fn.isString(v) ){
			return isNaN( v*1 ) ? null : v * 1
		}else{
			return null
		}
	},
	getSplitNum : v => {
		let rs = null
		if( window.$fn.isNumber(v) ){
			rs = v.toString()
		}else if( window.$fn.isString(v)  ){
			rs = isNaN( +v ) ? null : v.toString()
		}
		return rs ? rs.replace(/\B(?=(\d{3})+(?!\d))/g, ',') : rs
	},
	getSplitMoney : v => {
		let rs = null
		if( window.$fn.isNumber(v) ){
			rs = v.toFixed(2).toString()
		}else if( window.$fn.isString(v) ){
			if( v.includes(',') ){
				return v
			}
			rs = +v
			rs = isNaN( rs ) ? null : rs.toFixed(2).toString()
		}
		return rs ? rs.replace(/\B(?=(\d{3})+(?!\d))/g, ',') : rs
	},
	// 含小数四舍五入，保留两位小数
	getDec(num, n=2) {
		const f = Math.pow(10, n)
		const m = Math.round( num * f ) / f
		return parseFloat( m.toFixed(n) )
	},
	getDecMoney(num, n=2) {
		const f = Math.pow(10, n)
		const m = Math.round( num * f ) / f
		return m.toFixed(n)
	}
}
export default Index